﻿using System;

namespace ConsecutiveNumbersSum
{

    public class Solution
    {
        /// <summary>
        /// Given a positive integer N,
        /// how many ways can we write it as a sum of consecutive positive integers?
        /// </summary>
        /// <param name="N">positive integer</param>
        /// <returns>sum of consecutive positive integers</returns>
        public int ConsecutiveNumbersSum(int N)
        {
            /*
             * N = (i+1)+(i+2)+...+(i+k)
             * N = k*i+(1+k)*k/2
             */

            // N可分解为两个数的乘积和等差数列之和

            int count = 0;
            for (int i = 1; i <= N; i++)
            {
                // 等差数列的和
                int sequence = i * (i - 1) / 2;
                if (sequence >= N)
                {
                    break;
                }

                // i和k的乘积
                int product = N - sequence;
                if (product % i == 0)
                {
                    count += 1;

                    //for (int j = 1; j <= product / i; j++)
                    //{
                    //    Console.Write($"{i + j} ");
                    //}
                    //Console.WriteLine();
                }
            }

            return count;
        }
    }
}